package com.qcby.mycommunity_003.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.mycommunity_003.entity.Community;
import com.qcby.mycommunity_003.mapper.CommunityMapper;
import com.qcby.mycommunity_003.mapper.PersonMapper;
import com.qcby.mycommunity_003.service.CommunityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.mycommunity_003.vo.CommVo;
import com.qcby.mycommunity_003.vo.CommunityVo;
import com.qcby.mycommunity_003.vo.PageVo;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-03-25
 */
@Service
public class CommunityServiceImpl extends ServiceImpl<CommunityMapper, Community> implements CommunityService {

    @Autowired
    private CommunityMapper communityMapper;

    @Autowired
    private PersonMapper personMapper;

    /**
     * 社区分页查询，使用mybatisplus内置的分页功能
     * 需要配置分页拦截器
     * @param commVo
     * @return
     */
    @Override
    public PageVo ComList(CommVo commVo) {
      //构造mybatisplue查询所需的page对象
        Page<Community> page = new Page<>(commVo.getPage(), commVo.getLimit());
        //构造条件构造器
        QueryWrapper<Community> queryWrapper=new QueryWrapper<>();
       queryWrapper.like(StringUtils.isNotBlank(commVo.getCommunityName()), "community_name", commVo.getCommunityName());
       //调用mybatisplus内置的分页
        Page<Community> resultPage = communityMapper.selectPage(page, queryWrapper);
        //下面进行数据的封装，注意：使用pagevo不用map这样在其它分页查询时还能调用
        PageVo pageVo=new PageVo();
        pageVo.setTotalCount(resultPage.getTotal());
        pageVo.setCurrPage(resultPage.getCurrent());
        pageVo.setPageSize(resultPage.getSize());
        pageVo.setTotalPage(resultPage.getPages());
        //对list进行封装
        List<CommunityVo> list=new ArrayList();
        for (Community record : resultPage.getRecords()) {
            CommunityVo vo=new CommunityVo();
            //使用框架工具赋值相同属性名的值,别把参数写反了：这是把第一个赋给第二个的意思
            BeanUtils.copyProperties(record,vo);
            vo.setPersonCnt(personMapper.getCountBycomminityId(commVo.getCommunityId()));
            list.add(vo);
        }
        pageVo.setList(list);
        return pageVo;
    }
}
